/**
 * @fileoverview 伪装浏览器指纹的示例脚本
 * @description 本脚本通过覆盖浏览器属性、函数和方法来干扰指纹采集。
 */

// 1. 覆盖User-Agent和平台信息
Object.defineProperty(navigator, 'userAgent', {
    get: () => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
});
Object.defineProperty(navigator, 'platform', {
    get: () => 'Win32'
});
Object.defineProperty(navigator, 'language', {
    get: () => 'zh-CN'
});
Object.defineProperty(navigator, 'languages', {
    get: () => ['zh-CN', 'en-US']
});

// 2. 覆盖屏幕尺寸和颜色深度
Object.defineProperty(screen, 'width', { get: () => 1920 });
Object.defineProperty(screen, 'height', { get: () => 1080 });
Object.defineProperty(screen, 'colorDepth', { get: () => 24 });

// 3. 覆盖时区
Intl.DateTimeFormat.prototype.resolvedOptions = (() => {
    const original = Intl.DateTimeFormat.prototype.resolvedOptions;
    return function() {
        const options = original.call(this);
        options.timeZone = 'Asia/Shanghai';
        return options;
    };
})();

// 4. 伪装Canvas指纹
const toDataURLOriginal = HTMLCanvasElement.prototype.toDataURL;
HTMLCanvasElement.prototype.toDataURL = function(type, encoderOptions) {
    const context = this.getContext('2d');
    context.fillStyle = 'rgba(0,0,0,0.1)'; // 微小干扰
    context.fillRect(0, 0, 1, 1);
    return toDataURLOriginal.apply(this, arguments);
};

// 5. 伪装WebGL指纹
const getParameterOriginal = WebGLRenderingContext.prototype.getParameter;
WebGLRenderingContext.prototype.getParameter = function(parameter) {
    // 干扰UNMASKED_VENDOR_WEBGL 和 UNMASKED_RENDERER_WEBGL
    if (parameter === 37445) return 'Intel Inc.';
    if (parameter === 37446) return 'Intel Iris OpenGL Engine';
    return getParameterOriginal.apply(this, arguments);
};

// 6. 伪装Audio指纹
const AudioContextOriginal = window.AudioContext || window.webkitAudioContext;
window.AudioContext = function(...args) {
    const context = new AudioContextOriginal(...args);
    const originalGetChannelData = context.createAnalyser;
    context.createAnalyser = function() {
        const analyser = originalGetChannelData.apply(this, arguments);
        const originalGetFloatTimeDomainData = analyser.getFloatTimeDomainData;
        analyser.getFloatTimeDomainData = function(array) {
            // 添加微小噪声
            originalGetFloatTimeDomainData.call(this, array);
            for (let i = 0; i < array.length; i++) {
                array[i] += (Math.random() - 0.5) * 1e-5;
            }
        };
        return analyser;
    };
    return context;
};

// 7. 伪装插件和mimeTypes
Object.defineProperty(navigator, 'plugins', {
    get: () => [{ name: 'Fake Plugin', description: 'Fake Plugin Description' }]
});
Object.defineProperty(navigator, 'mimeTypes', {
    get: () => [{ type: 'application/x-fake', description: 'Fake MIME Type', suffixes: 'fake' }]
});

// 8. 防WebRTC泄漏
if (window.RTCPeerConnection) {
    const originalRTCPeerConnection = window.RTCPeerConnection;
    window.RTCPeerConnection = function(...args) {
        const pc = new originalRTCPeerConnection(...args);
        pc.createDataChannel = function() { return null; }; // 禁用数据通道
        return pc;
    };
}
